package com.clouddong.exterapi.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ResourceBundle;

public class TxtRead{

   public static void main(String args[]) {  
       String inputPath="D:\\TxtTest\\input.txt";
       String outPath="D:\\TxtTest\\output.txt";
       ReadAndWrite("16",inputPath,outPath);

    }  
    
    public static void ReadAndWrite(String cbiid,String inputPath,String outPath){
        
//        
//        ResourceBundle rb = ResourceBundle.getBundle("params");
//        String param1 = rb.getString("param1");
//        String param2 = rb.getString("param2");
//        String param3 = rb.getString("param3");
        
        try { // 防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw  

            /* 读入TXT文件变量声明 */  
            String pathname =inputPath; // 绝对路径或相对路径都可以，这里是绝对路径，写入文件时演示相对路径  
            File filename = new File(pathname); // 要读取以上路径的input。txt文件  
            InputStreamReader reader = new InputStreamReader(  
                    new FileInputStream(filename)); // 建立一个输入流对象reader  
            BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言  
            String line = "";  
            line = br.readLine();  
            /* 写入Txt文件变量声明 */  
            File writename = new File(outPath); // 相对路径，如果没有则要建立一个新的output.txt文件  
            writename.createNewFile(); // 创建新文件  
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));  
            while (line != null) {  
                System.out.println(line);
                
                String[] lineReads=line.split("/");
                String ip = lineReads[0];
                int mask = Integer.parseInt(lineReads[1]);
                //TestDemo testDemo=new TestDemo();
                long start = IPConvertUtil.ipToLong(ip);          // 地址
                long number = IPConvertUtil.ipCount(mask);        // ip地址个数
                long lmask = IPConvertUtil.maskToLong(mask);      // 掩码
                
                String startIp = (IPConvertUtil.longToIP(start & lmask)); //开始ip
                String endIp = (IPConvertUtil.longToIP((start & lmask) + number)); //结束ip
                
                long startIpLong=IPConvertUtil.ipToLong(startIp); //开始ip(10进制)
                long endIpLong=IPConvertUtil.ipToLong(endIp);  //结束ip(10进制)
                
                String wtxts="'"+startIp+"'"+","+"'"+endIp+"'"+","+"'"+startIp+"'"+","+"'"+mask+"'"+","+"'"+startIpLong+"'"+","+"'"+endIpLong+"'";
                //String wtxtsT=param1+cbiid+param2+wtxts+param3;
                out.write(wtxts+"\r\n"); // \r\n即为换行
                line = br.readLine(); // 一次读入一行数据  
                
            }    
            out.flush(); // 把缓存区内容压入文件  
            out.close(); // 最后记得关闭文件 
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
}
